package com.yx1996.test;

import com.yx1996.dao.UserDao;
import com.yx1996.dao.impl.UserDaoImpl;
import com.yx1996.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.InputStream;

/** 测试类充当userService层调用userDao */
public class test1Dao {

    private SqlSessionFactory sqlSessionFactory;

    @BeforeEach
    // 在测试之前加载主配置文件获得sessionfactory
    public void before() throws IOException {
        // 加载主配置文件
        InputStream resource = Resources.getResourceAsStream("mybatis.xml");
        // 获得mybatis的sessionFactory
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(resource);
    }

    @Test
    public void fun1() {

        UserDao userDao = new UserDaoImpl(sqlSessionFactory);
        User userById = userDao.findUserById(10);
        System.out.println(userById);
    }
}
